/**
 * 右键菜单
 * Created by kedong on 2017/7/20 0020.
 */
$(function () {

    var contextmenu_items = [
        {
            text: '设为地图中心',
            classname: 'bold',
            icon: ctx + '/static/images/map/center.png',
            callback: center
        },
        {
            text: '搜索附近的用户轨迹',
            classname: 'bold',
            icon: ctx + '/static/images/map/search.png',
            callback: search
        },
        {
            text: '搜索附近的系统轨迹',
            classname: 'bold',
            icon: ctx + '/static/images/map/search.png',
            callback: searchSys
        },
        {
            text: '添加标记',
            icon: ctx + '/static/images/map/pin_drop.png',
            callback: marker
        },
        '-' // this is a separator
    ];

    var contextmenu = new ContextMenu({
        width: 180,
        items: contextmenu_items
    });
    map.addControl(contextmenu);

    var removeMarkerItem = {
        text: '删除',
        classname: 'marker',
        callback: removeMarker
    };

    contextmenu.on('open', function (evt) {
        if (is_drawing()) {
            return;
        }

        var feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
            return ft;
        });
        if (feature) {
            contextmenu.clear();
            removeMarkerItem.data = {
                marker: feature
            };
            contextmenu.push(removeMarkerItem);
        } else {
            contextmenu.clear();
            contextmenu.extend(contextmenu_items);
            contextmenu.extend(contextmenu.getDefaultItems());
        }
    });

    map.on('pointermove', function (e) {
        if (e.dragging) return;

        var pixel = map.getEventPixel(e.originalEvent);
        var hit = map.hasFeatureAtPixel(pixel);
        // console.log('此处是否有元素：', hit);
        map.getTargetElement().style.cursor = hit ? 'pointer' : 'auto';
    });

    // from https://github.com/DmitryBaranovskiy/raphael
    function elastic(t) {
        return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
    }

    function center(obj) {
        view.animate({
            duration: 700,
            easing: elastic,
            center: obj.coordinate
        });
    }

    function removeMarker(obj) {
        var feature = obj.data.marker;
        removeFeatures(feature);
    }

    function marker(obj, isSearch, type) {
        var coord4326 = obj.coordinate,
            template = '({x} | {y})',
            iconStyle = new ol.style.Style({
                image: new ol.style.Icon({
                    anchor: [0.5, 1],
                    scale: 1,
                    src: ctx + '/static/images/map/marker-blue.png'
                }),
                text: new ol.style.Text({
                    offsetY: 25,
                    // text: ol.coordinate.format(coord4326, template, 4),
                    font: '15px Open Sans,sans-serif',
                    fill: new ol.style.Fill({color: '#111'}),
                    stroke: new ol.style.Stroke({color: '#eee', width: 2})
                })
            }),
            feature = new ol.Feature({
                geometry: new ol.geom.Point(obj.coordinate)
            });

        feature.setStyle(iconStyle);
        if (isSearch) {
            addFeaturesToSearchOverlay([feature]);
        } else {
            addFeatures([feature]);
        }
        $.publish("select:on");
    }

    /**
     * 搜索附近的轨迹
     * @param obj
     */
    function search(obj) {
        var coordinate = obj.coordinate;
        var data = {
            type: 1,
            lng: coordinate[0],
            lat: coordinate[1]
        };
        $.get('searchUser', data, function (data) {
            if (data.length > 0) {
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    var userPath = data[i];
                    var gpx = userPath.gpx;
                    if (gpx) {
                        loadGpx(gpx, 1, userPath.id, userPath.name);
                    }
                }
            } else {
                alert("该点附近暂无轨迹")
            }
        });
        marker(obj, true, 1);
    }

    function searchSys(obj) {
        var coordinate = obj.coordinate;
        var data = {
            type: 1,
            lng: coordinate[0],
            lat: coordinate[1]
        };
        $.get('searchSysPath', data, function (data) {
            if (data.length > 0) {
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    var sysPath = data[i];
                    var geojson = sysPath.geojson;
                    if (geojson) {
                        loadGeoJson(geojson, 2, sysPath.id, sysPath.name);
                    }
                }
            } else {
                alert("该点附近暂无轨迹")
            }
        });
        marker(obj, true, 2);
    }
});